package com.yh.netty.c1;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author MtRider
 * @version 1.0
 * @description BinarySearch 红旗0，白棋1
 * @GiteeAndGithub MtRider
 * @date 2022/3/2 22:29
 */
@Slf4j
public class BinarySearchDemo {
    public static void main(String[] args) {
        List<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(0);
        arrayList.add(0);
        arrayList.add(0);
        arrayList.add(1);
        arrayList.add(0);
        List<Integer> list = binarySearch(arrayList);
        log.debug("1.排序后 {}",arrayList);
        List<Integer> binarySearch2 = binarySearch2(arrayList);
        log.debug("2.排序后 {}",binarySearch2);
    }
    //method 1 红旗0，白棋非0正数
    //list.toArray(...);Arrays.asList(...);
    public static List<Integer> binarySearch(List<Integer> list){
        Collections.sort(list);
        return list;
    }

    //method 2 红旗0，白棋非0正数
    //快速排序
    public static List<Integer> binarySearch2(List<Integer> list){
        Integer[] array = (Integer[]) list.toArray(new Integer[list.size()]);
        int length=array.length;
        int low = 0, high = length - 1;
        int temp;
        while (low < high)
        {
            while (array[low] == 0 && low < high)
                low++;
            while (array[high] > 0 && low < high)
                high--;
            temp = array[low];
            array[low] = array[high];
            array[high] = temp;
        }
        return Arrays.asList(array);
    }
}
